﻿using HelixToolkit.Wpf;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
using System.Windows.Media;
using ReactiveUI.Fody.Helpers;
using System.Collections.ObjectModel;
using WPF_HelixToolkit_Demo.Models;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Windows.Controls;
using System.Windows.Input;
using Prism.Commands;
using System.Windows;

namespace WPF_HelixToolkit_Demo.ViewModels
{
    internal class MainWindowViewModel : ReactiveObject
    {
        [Reactive]
        public ObservableCollection<RotorModelParameter> ParameterList {  get; set; }

        [Reactive]
        public Model3D Model { get; set; }

        [Reactive]
        public Point3D GridLinesCenter { get; set; } = new Point3D(0, 0, 0);
        [Reactive]
        public double GridLinesLength { get; set; } = 0;
        [Reactive]
        public double GridLinesWidth { get; set; } = 0;
        [Reactive]
        public Point3D CameraPosition { get; set; } = new Point3D(0, 0, 0);

        [Reactive]
        public Vector3D CameraLookDirection { get; set; } = new Vector3D(0, 10, -10);

        [Reactive]
        public Vector3D CameraUpDirection { get; set; } = new Vector3D(0,0,1);

        [Reactive]
        public int ListSize { get; set; } = 0;

        public ICommand IncreaseCommand { get;}

        public ICommand DecreaseCommand { get;}

        public ICommand DeleteCommand { get; set; }

        public MainWindowViewModel()
        {
            IncreaseCommand = new DelegateCommand(ExecuteIncreaseCommand);
            DeleteCommand = new DelegateCommand<RotorModelParameter>(ExecuteDecreaseCommand);
            ParameterList = new ObservableCollection<RotorModelParameter>();
            ParameterList.CollectionChanged += CollectionChangedEventHandler;
        }

        private void ExecuteIncreaseCommand()
        {
            if (ListSize < 10) 
            {
                var newParameter = new RotorModelParameter { BaseRadius = 5.0, TopRadius = 5.0, Height = 10.0 };
                ParameterList.Add(newParameter);
                newParameter.PropertyChanged += CollectionItemChangedEventHandler;
            }
        }
        private void ExecuteDecreaseCommand(RotorModelParameter rotor)
        {
            if (rotor != null)
            {
                ParameterList.Remove(rotor);
            }
            else 
            {
                if (ParameterList.Count >= 1)
                {
                    var lastParameter = ParameterList[ParameterList.Count - 1];
                    lastParameter.PropertyChanged -= CollectionItemChangedEventHandler;
                    ParameterList.RemoveAt(ParameterList.Count - 1);
                }
            }
        }
        private void CollectionChangedEventHandler(object sender, NotifyCollectionChangedEventArgs e)
        {
            ListSize = ParameterList.Count;
            CreateModel();
        }
        private void CollectionItemChangedEventHandler(object sender, PropertyChangedEventArgs e)
        {
            CreateModel();
        }
        private void CreateModel()
        {
            
            //创建几何模型
            var modelGroup = new Model3DGroup();
            var meshBuilder = new MeshBuilder(false, false);
            int thetaDiv = 32;

            double maxRadius = ParameterList.Any() ? ParameterList.Max(p => Math.Max(p.BaseRadius, p.TopRadius)):0;
            double size = maxRadius * 2;

            double sumHeight = 0;

            Point3D startOrigin = new Point3D(0, 0, maxRadius);

            foreach (var coneParams in ParameterList)
            {
                meshBuilder.AddCone(startOrigin, new Vector3D(1, 0, 0), coneParams.BaseRadius, coneParams.TopRadius, coneParams.Height, true, true, thetaDiv);
                sumHeight += coneParams.Height;
                startOrigin = new Point3D(startOrigin.X + coneParams.Height, startOrigin.Y, startOrigin.Z);
                
            }
            // 设置材质
            var mesh = meshBuilder.ToMesh();
            var greenMaterial = MaterialHelper.CreateMaterial(Colors.DimGray,0.9);

            string resourcePath = "pack://application:,,,/WPF_HelixToolkit_Demo;component/Resource/材质1.jpg";
            var uri = new Uri(resourcePath, UriKind.Absolute);
            var metalMaterial = MaterialHelper.CreateImageMaterial(resourcePath, 1, UriKind.Absolute, true);

            var insideMaterial = MaterialHelper.CreateMaterial(Colors.Yellow);
            // 将模型添加到 Viewport3D 中显示
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Material = greenMaterial, BackMaterial = insideMaterial });
            Model = modelGroup;

            // 设置网格线
            GridLinesWidth = size;
            GridLinesLength = sumHeight;
            GridLinesCenter = new Point3D(sumHeight/2, 0, 0);
            // 设置相机位置
            CameraPosition = new Point3D(sumHeight / 2, -(size > sumHeight ? size * 2.5 : sumHeight*1.6), size > sumHeight ? size * 3 : sumHeight*2);
            CameraLookDirection = new Vector3D(0, 10, -10);
            CameraUpDirection = new Vector3D(0, 0, 1);
        }

    }
}
